package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.cderp.domain.*;
import com.ruoyi.cderp.mapper.*;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.service.ISettleSheetService;

/**
 * 供应商结算单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class SettleSheetServiceImpl implements ISettleSheetService
{
    @Autowired
    private SettleSheetMapper settleSheetMapper;

    @Autowired
    private SettleCheckSheetMapper settleCheckSheetMapper;
    @Autowired
    private SettleCheckSheetServiceImpl settleCheckSheetService;
    @Autowired
    private SettleFeeSheetMapper settleFeeSheetMapper;
    @Autowired
    private SettlePreSheetMapper settlePreSheetMapper;
    @Autowired
    private TblReceiveSheetMapper  tblReceiveSheetMapper;
    @Autowired
    private TblPurchaseReturnMapper  tblPurchaseReturnMapper;
    @Autowired
    private SettleFeeSheetDetailMapper settleFeeSheetDetailMapper;
    @Autowired
    private SettlePreSheetDetailMapper settlePreSheetDetailMapper;

    LoginUser loginUser = null;




    @Override
    public List<SettleSheetDetail> selectFeeSheetDetailList(Date startDate, Date endDate, String supplierId) {
        List<SettleSheetDetail> list = new ArrayList<>();
        List<SettleSheetDetail> settleFeeSheets = settleSheetMapper.selectSettleCheckSheet(startDate, endDate, supplierId);
        list.addAll(settleFeeSheets);
        return list;
    }

    /**
     * 查询供应商结算单
     *
     * @param id 供应商结算单主键
     * @return 供应商结算单
     */
    @Override
    public SettleSheet selectSettleSheetById(String id)
    {
        SettleSheet settleSheet = settleSheetMapper.selectSettleSheetById(id);
        settleSheet.setSettleSheetDetailList(settleSheetMapper.selectSettleSheetDetail(id));
        return settleSheet;
    }

    /**
     * 查询供应商结算单列表
     *
     * @param settleSheet 供应商结算单
     * @return 供应商结算单
     */
    @Override
    public List<SettleSheet> selectSettleSheetList(SettleSheet settleSheet)
    {
        return settleSheetMapper.selectSettleSheetList(settleSheet);
    }

    /**
     * 新增供应商结算单
     *
     * @param settleSheet 供应商结算单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSettleSheet(SettleSheet settleSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        settleSheet.setCreateById(loginUser.getUser().getUserId().toString());
        settleSheet.setCreateBy(loginUser.getUser().getUserName());
        settleSheet.setCreateTime(DateUtils.getNowDate());
        int rows = settleSheetMapper.insertSettleSheet(settleSheet);
        insertSettleSheetDetail(settleSheet);
        return rows;
    }

    /**
     * 修改供应商结算单
     *
     * @param settleSheet 供应商结算单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSettleSheet(SettleSheet settleSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        settleSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        settleSheet.setUpdateBy(loginUser.getUser().getUserName());
        settleSheet.setUpdateTime(DateUtils.getNowDate());
        //根据客户结算单id查询结算单详情
        List<SettleSheetDetail> settleSheetDetails = settleSheetMapper.selectSettleSheetDetail(settleSheet.getId());
        for (SettleSheetDetail settleSheetDetail : settleSheetDetails)
        {
            //实例化一个对账对象
            SettleCheckSheet settleCheckSheet = new SettleCheckSheet();
//赋值对账单状态
            settleCheckSheet.setSettleStatus(Long.valueOf(0));
//赋值对账单编号
            settleCheckSheet.setId(settleSheetDetail.getBizId());
//调用对账修改接口
            settleCheckSheetMapper.updateSettleCheckSheet(settleCheckSheet);



//调用查询对账详情接口
            SettleCheckSheet settleCheckSheetDetails = settleCheckSheetService.selectSettleCheckSheetById(settleSheetDetail.getBizId());
//循环遍历对账详情
            for(SettleCheckSheetDetail settleCheckSheetDetail:settleCheckSheetDetails.getSettleCheckSheetDetailList()) {
                //根据对账单详情中类型判断修改相对应的结算状态
                if (settleCheckSheetDetail.getBizType() == 1) {
                    SettleFeeSheet settleFeeSheet = new SettleFeeSheet();
                    settleFeeSheet.setSettleStatus(Long.valueOf(0));
                    settleFeeSheet.setId(settleCheckSheetDetail.getBizId());
                    settleFeeSheetMapper.updateSettleFeeSheet(settleFeeSheet);

                    //根据对账的详情中的客户收款单id查询收款单详情
                    List<SettleFeeSheetDetail> FeeSheetDetail = settleFeeSheetDetailMapper.selectSettleFeeSheetDetail(settleCheckSheetDetail.getBizId());
                    //循环客户收款单详情修改结算状态
                    for(SettleFeeSheetDetail settleFeeSheetDetail:FeeSheetDetail){
                        TblPurchaseReturn tblPurchaseReturn = new TblPurchaseReturn();
                        tblPurchaseReturn.setSettleStatus(Long.valueOf(0));
                        tblPurchaseReturn.setId(settleFeeSheetDetail.getOrderId());
                        tblPurchaseReturnMapper.updateTblPurchaseReturn(tblPurchaseReturn);
                    }
                }
                if (settleCheckSheetDetail.getBizType() == 2) {
                    SettlePreSheet settlePreSheet = new SettlePreSheet();
                    settlePreSheet.setSettleStatus(Long.valueOf(0));
                    settlePreSheet.setId(settleCheckSheetDetail.getBizId());
                    settlePreSheetMapper.updateSettlePreSheet(settlePreSheet);

                    //根据对账的详情中的客户收款单id查询收款单详情
                    List<SettlePreSheetDetail> PreSheetDetail = settlePreSheetDetailMapper.selectSettlePreSheetDetailOut(settleCheckSheetDetail.getBizId());
                    //循环客户收款单详情修改结算状态
                    for(SettlePreSheetDetail settlePreSheetDetail:PreSheetDetail){
                        TblReceiveSheet tblReceiveSheet = new TblReceiveSheet();
                        tblReceiveSheet.setSettleStatus(Long.valueOf(0));
                        tblReceiveSheet.setId(settlePreSheetDetail.getOrderId());
                        tblReceiveSheetMapper.updateTblReceiveSheet(tblReceiveSheet);
                    }
                }
            }
        }
        settleSheetMapper.deleteSettleSheetDetailBySheetId(settleSheet.getId());
        insertSettleSheetDetail(settleSheet);
        if(settleSheet.getStatus() == 3){
            //根据客户结算单id查询结算单详情
            List<SettleSheetDetail> settleSheetDetails1 = settleSheetMapper.selectSettleSheetDetail(settleSheet.getId());
            for (SettleSheetDetail settleSheetDetail : settleSheetDetails1) {
                //实例化一个对账对象
                SettleCheckSheet settleCheckSheet = new SettleCheckSheet();
                //赋值对账单状态
                settleCheckSheet.setSettleStatus(Long.valueOf(2));
                //赋值对账单编号
                settleCheckSheet.setId(settleSheetDetail.getBizId());
                //调用对账修改接口
                settleCheckSheetMapper.updateSettleCheckSheet(settleCheckSheet);


                //调用查询对账详情接口
                SettleCheckSheet settleCheckSheetDetails = settleCheckSheetService.selectSettleCheckSheetById(settleSheetDetail.getBizId());
                //循环遍历对账详情
                for (SettleCheckSheetDetail settleCheckSheetDetail : settleCheckSheetDetails.getSettleCheckSheetDetailList()) {
                    //根据对账单详情中类型判断修改相对应的结算状态
                    if (settleCheckSheetDetail.getBizType() == 1) {
                        SettleFeeSheet settleFeeSheet = new SettleFeeSheet();
                        settleFeeSheet.setSettleStatus(Long.valueOf(2));
                        settleFeeSheet.setId(settleCheckSheetDetail.getBizId());
                        settleFeeSheetMapper.updateSettleFeeSheet(settleFeeSheet);

                        //根据对账的详情中的客户收款单id查询收款单详情
                        List<SettleFeeSheetDetail> FeeSheetDetail = settleFeeSheetDetailMapper.selectSettleFeeSheetDetail(settleCheckSheetDetail.getBizId());
                        //循环客户收款单详情修改结算状态
                        for(SettleFeeSheetDetail settleFeeSheetDetail:FeeSheetDetail){
                            TblPurchaseReturn tblPurchaseReturn = new TblPurchaseReturn();
                            tblPurchaseReturn.setSettleStatus(Long.valueOf(2));
                            tblPurchaseReturn.setId(settleFeeSheetDetail.getOrderId());
                            tblPurchaseReturnMapper.updateTblPurchaseReturn(tblPurchaseReturn);
                        }
                    }
                    if (settleCheckSheetDetail.getBizType() == 2) {
                        SettlePreSheet settlePreSheet = new SettlePreSheet();
                        settlePreSheet.setSettleStatus(Long.valueOf(2));
                        settlePreSheet.setId(settleCheckSheetDetail.getBizId());
                        settlePreSheetMapper.updateSettlePreSheet(settlePreSheet);

                        //根据对账的详情中的客户收款单id查询收款单详情
                        List<SettlePreSheetDetail> PreSheetDetail = settlePreSheetDetailMapper.selectSettlePreSheetDetailOut(settleCheckSheetDetail.getBizId());
                        //循环客户收款单详情修改结算状态
                        for(SettlePreSheetDetail settlePreSheetDetail:PreSheetDetail){
                            TblReceiveSheet tblReceiveSheet = new TblReceiveSheet();
                            tblReceiveSheet.setSettleStatus(Long.valueOf(2));
                            tblReceiveSheet.setId(settlePreSheetDetail.getOrderId());
                            tblReceiveSheetMapper.updateTblReceiveSheet(tblReceiveSheet);
                        }
                    }
                }
            }
        }
        return settleSheetMapper.updateSettleSheet(settleSheet);
    }

    /**
     * 批量删除供应商结算单
     *
     * @param ids 需要删除的供应商结算单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettleSheetByIds(String[] ids)
    {
        //根据客户结算单id查询结算单详情
        List<SettleSheetDetail> settleSheetDetails = settleSheetMapper.selectSettleSheetDetail(ids[0]);
        for (SettleSheetDetail settleSheetDetail : settleSheetDetails)
        {
            //实例化一个对账对象
            SettleCheckSheet settleCheckSheet = new SettleCheckSheet();
//赋值对账单状态
            settleCheckSheet.setSettleStatus(Long.valueOf(0));
//赋值对账单编号
            settleCheckSheet.setId(settleSheetDetail.getBizId());
//调用对账修改接口
            settleCheckSheetMapper.updateSettleCheckSheet(settleCheckSheet);



//调用查询对账详情接口
            SettleCheckSheet settleCheckSheetDetails = settleCheckSheetService.selectSettleCheckSheetById(settleSheetDetail.getBizId());
//循环遍历对账详情
            for(SettleCheckSheetDetail settleCheckSheetDetail:settleCheckSheetDetails.getSettleCheckSheetDetailList()) {
                //根据对账单详情中类型判断修改相对应的结算状态
                if (settleCheckSheetDetail.getBizType() == 1) {
                    SettleFeeSheet settleFeeSheet = new SettleFeeSheet();
                    settleFeeSheet.setSettleStatus(Long.valueOf(0));
                    settleFeeSheet.setId(settleCheckSheetDetail.getBizId());
                    settleFeeSheetMapper.updateSettleFeeSheet(settleFeeSheet);

                    //根据对账的详情中的客户收款单id查询收款单详情
                    List<SettleFeeSheetDetail> FeeSheetDetail = settleFeeSheetDetailMapper.selectSettleFeeSheetDetail(settleCheckSheetDetail.getBizId());
                    //循环客户收款单详情修改结算状态
                    for(SettleFeeSheetDetail settleFeeSheetDetail:FeeSheetDetail){
                        TblPurchaseReturn tblPurchaseReturn = new TblPurchaseReturn();
                        tblPurchaseReturn.setSettleStatus(Long.valueOf(0));
                        tblPurchaseReturn.setId(settleFeeSheetDetail.getOrderId());
                        tblPurchaseReturnMapper.updateTblPurchaseReturn(tblPurchaseReturn);
                    }
                }
                if (settleCheckSheetDetail.getBizType() == 2) {
                    SettlePreSheet settlePreSheet = new SettlePreSheet();
                    settlePreSheet.setSettleStatus(Long.valueOf(0));
                    settlePreSheet.setId(settleCheckSheetDetail.getBizId());
                    settlePreSheetMapper.updateSettlePreSheet(settlePreSheet);

                    //根据对账的详情中的客户收款单id查询收款单详情
                    List<SettlePreSheetDetail> PreSheetDetail = settlePreSheetDetailMapper.selectSettlePreSheetDetailOut(settleCheckSheetDetail.getBizId());
                    //循环客户收款单详情修改结算状态
                    for(SettlePreSheetDetail settlePreSheetDetail:PreSheetDetail){
                        TblReceiveSheet tblReceiveSheet = new TblReceiveSheet();
                        tblReceiveSheet.setSettleStatus(Long.valueOf(0));
                        tblReceiveSheet.setId(settlePreSheetDetail.getOrderId());
                        tblReceiveSheetMapper.updateTblReceiveSheet(tblReceiveSheet);
                    }
                }
            }
        }
        settleSheetMapper.deleteSettleSheetDetailBySheetIds(ids);
        return settleSheetMapper.deleteSettleSheetByIds(ids);
    }

    /**
     * 删除供应商结算单信息
     *
     * @param id 供应商结算单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettleSheetById(String id)
    {
        settleSheetMapper.deleteSettleSheetDetailBySheetId(id);
        return settleSheetMapper.deleteSettleSheetById(id);
    }

    /**
     * 新增供应商结算单明细信息
     *
     * @param settleSheet 供应商结算单对象
     */
    public void insertSettleSheetDetail(SettleSheet settleSheet)
    {
        List<SettleSheetDetail> settleSheetDetailList = settleSheet.getSettleSheetDetailList();
        String id = settleSheet.getId();
        if (StringUtils.isNotNull(settleSheetDetailList))
        {
            List<SettleSheetDetail> list = new ArrayList<SettleSheetDetail>();
            for (SettleSheetDetail settleSheetDetail : settleSheetDetailList)
            {
                settleSheetDetail.setSheetId(id);
                //随机生成组合商品编号
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String formattedDate = sdf.format(new Date());
                Random random = new Random();
                int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
                settleSheetDetail.setId("160" + formattedDate+randomNumber);
                list.add(settleSheetDetail);


                //实例化一个对账对象
                SettleCheckSheet settleCheckSheet = new SettleCheckSheet();
                //赋值对账单状态
                settleCheckSheet.setSettleStatus(Long.valueOf(1));
                //赋值对账单编号
                settleCheckSheet.setId(settleSheetDetail.getBizId());
                //调用对账修改接口
                settleCheckSheetMapper.updateSettleCheckSheet(settleCheckSheet);



                //调用查询对账详情接口
                SettleCheckSheet settleCheckSheetDetails = settleCheckSheetService.selectSettleCheckSheetById(settleSheetDetail.getBizId());
                //循环遍历对账详情
                for(SettleCheckSheetDetail settleCheckSheetDetail:settleCheckSheetDetails.getSettleCheckSheetDetailList()){
                    //根据对账单详情中类型判断修改相对应的结算状态
                    if (settleCheckSheetDetail.getBizType() == 1) {
                        SettleFeeSheet settleFeeSheet = new SettleFeeSheet();
                        settleFeeSheet.setSettleStatus(Long.valueOf(1));
                        settleFeeSheet.setId(settleCheckSheetDetail.getBizId());
                        settleFeeSheetMapper.updateSettleFeeSheet(settleFeeSheet);

                        //根据对账的详情中的客户收款单id查询收款单详情
                        List<SettleFeeSheetDetail> FeeSheetDetail = settleFeeSheetDetailMapper.selectSettleFeeSheetDetail(settleCheckSheetDetail.getBizId());
                        //循环客户收款单详情修改结算状态
                        for(SettleFeeSheetDetail settleFeeSheetDetail:FeeSheetDetail){
                            TblPurchaseReturn tblPurchaseReturn = new TblPurchaseReturn();
                            tblPurchaseReturn.setSettleStatus(Long.valueOf(1));
                            tblPurchaseReturn.setId(settleFeeSheetDetail.getOrderId());
                            tblPurchaseReturnMapper.updateTblPurchaseReturn(tblPurchaseReturn);
                        }
                    }
                    if (settleCheckSheetDetail.getBizType() == 2) {
                        SettlePreSheet settlePreSheet = new SettlePreSheet();
                        settlePreSheet.setSettleStatus(Long.valueOf(1));
                        settlePreSheet.setId(settleCheckSheetDetail.getBizId());
                        settlePreSheetMapper.updateSettlePreSheet(settlePreSheet);

                        //根据对账的详情中的客户收款单id查询收款单详情
                        List<SettlePreSheetDetail> PreSheetDetail = settlePreSheetDetailMapper.selectSettlePreSheetDetailOut(settleCheckSheetDetail.getBizId());
                        //循环客户收款单详情修改结算状态
                        for(SettlePreSheetDetail settlePreSheetDetail:PreSheetDetail){
                            TblReceiveSheet tblReceiveSheet = new TblReceiveSheet();
                            tblReceiveSheet.setSettleStatus(Long.valueOf(1));
                            tblReceiveSheet.setId(settlePreSheetDetail.getOrderId());
                            tblReceiveSheetMapper.updateTblReceiveSheet(tblReceiveSheet);
                        }
                    }
                }
            }
            if (list.size() > 0)
            {
                settleSheetMapper.batchSettleSheetDetail(list);
            }
        }
    }
}
